package szwolaiyun.paireach.com.szwolaizhuang.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

import com.zbar.lib.CaptureActivity;

import szwolaiyun.paireach.com.szwolaizhuang.util.MessageInfoUtil;

/**
 * 扫描条形码的fragment
 * Created by Administrator on 2016/7/19.
 */
public class ScanBarCodeFragment extends Fragment {
    private View view;
    private Handler handler;
    //标记要跳转到哪个fragment
    private int fragmentMark = MessageInfoUtil.TO_HOME;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startActivityForResult(new Intent(getActivity(), CaptureActivity.class), 1);
    }

//    Camera.PreviewCallback previewCb = new Camera.PreviewCallback() {
//        public void onPreviewFrame(byte[] data, Camera camera) {
//            Camera.Parameters parameters = camera.getParameters();
//            Camera.Size size = parameters.getPreviewSize();
//            //初始化预览图像
//            Image barcode = new Image(size.width, size.height, "Y800");
//            //填充预览图像数据
//            barcode.setData(data);
////解析预览图像
//            int result = scanner.scanImage(barcode);
//
//            if (result != 0) {
//                previewing = false;
//                mCamera.setPreviewCallback(null);
//                mCamera.stopPreview();
////得到解析结果
//                SymbolSet syms = scanner.getResults();
//
////				playBeepSoundAndVibrate();// 播放声音和振动代表成功获取二维码
//
//                for (Symbol sym : syms) {
//                    Log.e("0011", "扫描结果" + sym.getData());
//                    // 将扫描后的信息返回
//                    releaseCamera();
//                    handler.sendEmptyMessage(fragmentMark);
//                }
//            }
//        }
//    };

    public void setHandle(Handler handler) {
        this.handler = handler;
    }

    public void setFragmentMark(int fragmentMark) {
        this.fragmentMark = fragmentMark;
    }

    public void goBack() {
        handler.sendEmptyMessage(MessageInfoUtil.TO_HOME);
    }

//    @Override
//    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.cancel_scan:
//                releaseCamera();
//                switch (fragmentMark) {
//                    case MessageInfoUtil.TO_MAKE_SURE_START_WORK:
//                        handler.sendEmptyMessage(MessageInfoUtil.TO_HOME);
//                        break;
//                    case MessageInfoUtil.TO_MAKE_SURE_FINISH_WORK:
//                        handler.sendEmptyMessage(MessageInfoUtil.TO_WORKING);
//                        break;
//                }
//
//                break;
//        }
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
            //TODO
            handler.sendEmptyMessage(MessageInfoUtil.TO_WORKING);
        }
    }
}
